package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.util.List;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import bean.Log;
import bean.Mail;
import bean.MailResume;
import bean.User;
import dao.*;

/**
 * Servlet implementation class Login_Servlet
 */
@WebServlet("/Login_Servlet")
public class Login_Servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    /*public Login_Servlet() {
        super();
        // TODO Auto-generated constructor stub
    }*/

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		response.setContentType("text/html");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String user_id=request.getParameter("user_id");
		String password=request.getParameter("password");
		User user = new User();
		UserManagerDao userdao = new  UserManagerDaoImpl();
		boolean judgeuser = userdao.pwdIsTrue(password, user_id);
		boolean judgeadmin=userdao.admIsTrue(user_id);
		if(judgeuser==true){
			if(judgeadmin==true){
				response.sendRedirect("serverManage.jsp");
				//request.getRequestDispatcher("serverManage.jsp").forward(request, response);
				User user1=new User();
				HttpSession session = request.getSession();
				user1=userdao.getUser(user_id).get(0);
				user1.setPassword(null);
				session.setAttribute("user", user1);
			}
			else{
				response.sendRedirect("AdError.jsp");
				//request.getRequestDispatcher("AdError.jsp").forward(request, response);
			}
		}
		else{
			response.sendRedirect("Error.jsp");
//			request.getRequestDispatcher("Error.jsp").forward(request, response);
		}
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
